# routes/user_routes.py - 用户登录路由
from flask import Blueprint, jsonify, request
from models.user import User, UserModel

# 创建用户路由蓝图
user_bp = Blueprint('user_auth', __name__, url_prefix='/eduAi/api')

# 用户登录接口
@user_bp.route('/user_auth/login', methods=['POST'])
def login():
    """用户登录"""
    try:
        data = request.get_json()
        if not data or 'userName' not in data or 'passWord' not in data:
            return jsonify({
                'success': False,
                'message': '请提供用户名和密码'
            }), 400
            
        userName = data['userName']
        passWord = data['passWord']
        user_model = User()
        password = user_model.get_user_password(userName)
        if password and password == passWord:
            return jsonify({
                'success': True,
                'message': '登录成功',
                'code': 200
            })
        else:
            return jsonify({
                'success': False,
                'message': '用户名或密码错误，请检查后重试',
                'code': 401
            }), 401
    except Exception as e:
        return jsonify({
            'success': False,
            'message': '登录失败,服务器访问异常，请重试。',
            'error': str(e),
        }), 500
